package com.dhcc.core.modules.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.dict.IDictCache;
import com.dhcc.core.modules.system.entity.Dict;
import com.dhcc.core.modules.system.entity.Notice;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.core.modules.system.service.IDictService;
import com.dhcc.core.modules.system.warpper.DictWarpper;
import com.dhcc.core.modules.system.warpper.NoticeWrapper;

/**
 * 字典控制器
 *
 * @ClassName: DictController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:19:00
 */
@Controller
@RequestMapping("${sys.backendPath}/dict")
public class DictController extends BaseController {

    private String PREFIX = "/backend/system/dict/";

    @Autowired
    IDictService dictService;

    /**
     * 跳转到字典管理首页
     */
    @RequestMapping
    public String index() {
        return PREFIX + "dict.html";
    }
    /**
     * 跳转到添加字典
     */
    @RequestMapping("/index_add/{catId}")
    public String indexAdd(@PathVariable Long catId, Model model) {
        model.addAttribute("catId", catId);
        return PREFIX + "dict_dept.html";
    }

    /**
     * 跳转到添加字典
     */
    @Permission("dict:add")
    @RequestMapping("/dict_add")
    public String deptAdd() {
        return PREFIX + "dict_add.html";
    }
    /**
     * 跳转到添加字典
     */
    @Permission("dict:add")
    @RequestMapping("/dict_dept_add")
    public String dictDeptAdd() {

        return PREFIX + "dict_dept_add.html";
    }

    /**
     * 跳转到修改字典
     */
    @Permission("dict:update")
    @RequestMapping("/dict_edit/{dictId}")
    public String deptUpdate(@PathVariable Long dictId, Model model) {
        Dict dict = dictService.selectById(dictId);
        model.addAttribute("dict", new DictWarpper(dict).warp());
        List<Dict> subDicts = dictService.selectList(new EntityWrapper<Dict>().eq("PARENT_ID", dictId));
        model.addAttribute("subDicts", subDicts);
        return PREFIX + "dict_edit.html";
    }
    /**
     * 跳转到修改字典
     */
    @Permission("dict:update")
    @RequestMapping("/dict_dept_edit/{dictId}")
    public String deptDeptUpdate(@PathVariable Long dictId, Model model) {
        Dict dict = dictService.selectById(dictId);
        model.addAttribute("dict", new DictWarpper(dict).warp());
        //model.addAttribute("deptName",dict.getd)
        List<Dict> subDicts = dictService.selectList(new EntityWrapper<Dict>().eq("PARENT_ID", dictId));
        model.addAttribute("subDicts", subDicts);
        return PREFIX + "dict_dept_edit.html";
    }

    /**
     * 新增字典
     *
     * @param dictValues
     *            格式例如 "1:启用;2:禁用;3:冻结"
     */
    @BussinessLog(value = "添加字典记录")
    @RequestMapping(value = "/add")
    @Permission("dict:add")
    @ResponseBody
    @CacheClear(IDictCache.CACHE_NAME)
    public Object add(Dict dict, String dictValues) {
        if (CommonUtil.isOneEmpty(dict.getName(), dict.getCode(), dictValues)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        this.dictService.addDict(dict, dictValues);
        return R.ok();
    }

    /**
     * 获取所有字典列表
     */
    @RequestMapping(value = "/list")
    @Permission("dict:list")
    @ResponseBody
    public Object list(Long categoryId,String condition,Long deptId) {

        List<Map<String, Object>> list = this.dictService.list(categoryId,condition,deptId);
        return super.warpObject(new DictWarpper(list));
    }

    /**
     * 获取所有字典列表
     */
    @RequestMapping(value = "/pageList")
    @ResponseBody
    public Object pageList(Long categoryId,String condition) {
        Page<Dict> page = new PageFactory<Dict>().defaultPage();
        List<Map<String, Object>> resultList = this.dictService.pageList(page,categoryId,condition);
        page.setRecords((List<Dict>) new DictWarpper(resultList).warp());
        return R.packForBT(page);
    }


    /**
     * 字典详情
     */
    @RequestMapping(value = "/detail/{dictId}")
    @Permission("dict:detail")
    @ResponseBody
    public Object detail(@PathVariable("dictId") Long dictId) {
        return new DictWarpper(dictService.selectById(dictId)).warp();
    }

    /**
     * 修改字典
     */
    @BussinessLog(value = "修改字典")
    @RequestMapping(value = "/update")
    @Permission("dict:update")
    @ResponseBody
    @CacheClear(IDictCache.CACHE_NAME)
    public Object update(Dict dict, String dictValues) {
        if (CommonUtil.isOneEmpty(dict.getId(), dict.getName(), dict.getCode(), dictValues)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        dictService.editDict(dict, dictValues);
        return R.ok();
    }

    /**
     * 删除字典记录
     */
    @BussinessLog(value = "删除字典记录")
    @RequestMapping(value = "/delete")
    @Permission("dict:delete")
    @ResponseBody
    @CacheClear(IDictCache.CACHE_NAME)
    public Object delete(@RequestParam Long dictId) {
        this.dictService.delteDict(dictId);
        return R.ok();
    }
}
